#!/bin/bash


EXPECTED_ARGS=1

if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Usage: `basename $0` <delay in seconds between starting server and starting client>"
  exit $E_BADARGS
fi

# create kv nodes
for i in {0..9}
	do
		CMD="java -jar ../mp2.jar services$i.ini topology.ini "
		echo $CMD 
		FIFO=/tmp/myfifo.$i
		rm -f $FIFO
		mkfifo $FIFO
		eval "($CMD) > $FIFO & "
		pids[$i]=$!
		cat $FIFO | tee $i.log &
	done

sleep $1s


# create and execute clients
for i in {0..1}
	do
		CMD="java -jar ../kvclient.jar topology.ini storageload"
		echo $CMD 
		FIFO=/tmp/myfifo.client$i
		rm -f $FIFO
		mkfifo $FIFO
		eval "($CMD) > $FIFO & "
		clientpids[$i]=$!
		cat $FIFO | tee client$i.log &
	done

CMD="java -jar ../kvclient.jar topology.ini storageload"
echo $CMD


# waith for clients
for i in "${clientpids[@]}"
	do
		wait $i
	done







#sleep 1s
#eval "($CMD) |  tee client.log & "
#eval "($CMD) |  tee client.log "

# kill kv nodes
for i in "${pids[@]}"
	do
		kill $i
	done
